package com.sfy.login.third;


import com.sfy.login.util.TimeUtil;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;


public class MailUtil {
    /**
     * 使用网易邮箱发送邮件
     *
     * @param to   给谁发
     */
    public static boolean sendMail(String to, Integer code) {
        //创建连接对象 连接到邮件服务器
        Properties properties = new Properties();
        //设置发送邮件的基本参数
        //发送邮件服务器

        properties.put("mail.smtp.host", "smtp.163.com");
        //发送端口
        properties.put("mail.smtp.port", "25");
        properties.put("mail.smtp.auth", "true");

        //设置发送邮件的账号和密码
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected javax.mail.PasswordAuthentication getPasswordAuthentication() {

//                return new javax.mail.PasswordAuthentication("你的网易邮箱", "你的网易邮箱授权码");
                return new javax.mail.PasswordAuthentication("g13523667227@163.com", "HCEOUYMVYLLLLUME");
            }
        });

        //创建邮件对象
        Message message = new MimeMessage(session);
        //设置发件人
        try {
            message.setFrom(new InternetAddress("g13523667227@163.com"));
            //设置收件人
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
            //设置主题
            message.setSubject("汇佳优选");
            //设置邮件正文  第二个参数是邮件发送的类型
            message.setContent("<!DOCTYPE html>\n" +
                                             "<html>\n" +
                                             "<head>\n" +
                                             "<title></title>\n" +
                                             "<style>\n" +
                                             ".qmbox {\n" +
                                             "\tpadding: 0;\n" +
                                             "}\n" +
                                             ".qm_con_body_content {\n" +
                                             "\theight: auto;\n" +
                                             "\tmin-height: 100px;\n" +
                                             "\t_height: 100px;\n" +
                                             "\tword-wrap: break-word;\n" +
                                             "\tfont-size: 14px;\n" +
                                             "\tfont-family: \"lucida Grande\", Verdana, \"Microsoft YaHei\";\n" +
                                             "}\n" +
                                             ".body {\n" +
                                             "\tline-height: 170%;\n" +
                                             "}\n" +
                                             "BODY {\n" +
                                             "\tfont-family: \"lucida Grande\", Verdana, \"Microsoft YaHei\";\n" +
                                             "\tfont-size: 12px;\n" +
                                             "\t-webkit-font-smoothing: subpixel-antialiased;\n" +
                                             "}\n" +
                                             "BODY {\n" +
                                             "\tmargin: 0;\n" +
                                             "\tpadding: 0;\n" +
                                             "}\n" +
                                              "BODY {\n" +
                                             "\tbackground-color: #fff;\n" +
                                             "\tfont-size: 12px;\n" +
                                             "}\n" +
                                             "BODY {\n" +
                                             "\tbackground: #fff;\n" +
                                             "}\n" +
                                             "BODY {\n" +
                                             "\tbackground: #fff;\n" +
                                             "\tcolor: #000;\n" +
                                             "\tfont-weight: normal;\n" +
                                             "\tfont-family: \"lucida Grande\", Verdana, \"Microsoft YaHei\";\n" +
                                             "\tpadding: 0 7px 6px 4px;\n" +
                                             "\tmargin: 0;\n" +
                                             "}\n" +
                                             "HTML {\n" +
                                             "\ttop: 0px;\n" +
                                             "}\n" +
                                             ".body P {\n" +
                                             "\tline-height: 170%;\n" +
                                             "}\n" +
                                             "</style>\n" +
                                             "</head>\n" +
                                             "\n" +
                                             "<BODY mu=\"mu\" md=\"md\" module=\"qmReadMail\" context=\"ZC1912-rQ7uXSy7P7HThxdLFCOuY92\"><DIV class=mailcontainer id=qqmail_mailcontainer>\n" +
                                             "<DIV id=mainmail style=\"MARGIN-BOTTOM: 12px; POSITION: relative; Z-INDEX: 1\">\n" +
                                             "<DIV class=body id=contentDiv style=\"FONT-SIZE: 14px; HEIGHT: auto; POSITION: relative; ZOOM: 1; PADDING-BOTTOM: 10px; PADDING-TOP: 15px; PADDING-LEFT: 15px; Z-INDEX: 1; LINE-HEIGHT: 1.7; PADDING-RIGHT: 15px\" οnmοuseοver=getTop().stopPropagation(event); οnclick=\"getTop().preSwapLink(event, 'html', 'ZC1912-rQ7uXSy7P7HThxdLFCOuY92');\">\n" +
                                             "<DIV id=qm_con_body>\n" +
                                             "<DIV class=\"qmbox qm_con_body_content qqmail_webmail_only\" id=mailContentContainer>\n" +
                                             "<DIV class=main style=\"OVERFLOW: hidden; WIDTH: 100%; BACKGROUND-COLOR: #f7f7f7\">\n" +
                                             "<DIV class=content style=\"BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; BACKGROUND: #ffffff; BORDER-BOTTOM: #cccccc 1px solid; PADDING-BOTTOM: 10px; PADDING-TOP: 10px; PADDING-LEFT: 25px; BORDER-LEFT: #cccccc 1px solid; MARGIN: 50px; PADDING-RIGHT: 25px\">\n" +
                                             "<DIV class=header style=\"MARGIN-BOTTOM: 30px\">\n" +
                                             "<P>亲爱的用户：</P></DIV>\n" +
                                             "<P>您好！您正在进行邮箱验证，本次请求的验证码为：</P>\n" +
                                             "<P><SPAN style=\"FONT-SIZE: 18px; FONT-WEIGHT: bold; COLOR: #f90\">"+ code +"</SPAN><SPAN style=\"COLOR: #000000\">(为了保障您帐号的安全性，请在5分钟内完成验证)</SPAN></P>\n" +
                                             "<DIV class=footer style=\"MARGIN-TOP: 30px\">\n" +
                                             "<P><SPAN style=\"BORDER-BOTTOM: #ccc 1px dashed; POSITION: relative; _display: inline-block\" t=\"5\" times=\"\" isout=\"0\">\n" + TimeUtil.TimeFormat(new Date()) + "</SPAN></P></DIV>\n" +
                                             "<DIV class=tip style=\"COLOR: #cccccc; TEXT-ALIGN: center\">该邮件为系统自动发送，请勿进行回复 </DIV></DIV></DIV></DIV></DIV></DIV></DIV></DIV></BODY>\n" +
                                             "</html>\n", "text/html;charset=UTF-8");
            //发送一封邮件
            Transport.send(message);
            return true;
        } catch (javax.mail.MessagingException e) {
            e.printStackTrace();
        }
        return false;
    }
}
